import { useEffect } from "react";
import { Spin, Space, Divider, Empty, message, Popconfirm } from "antd";
import { Card as SemiCard, Row, Col, List, ButtonGroup, Button } from "@douyinfe/semi-ui";
import { IconDelete } from "@douyinfe/semi-icons";
import { useModel } from "umi";
import { useReactive } from "ahooks";
import dayjs from "dayjs";

import useAllLogFiles from "../hooks/gateway/useAllLogFiles";
import useLogContent from "../hooks/gateway/useLogContent";
import useDeleteLogFile from "../hooks/gateway/useDeleteLogFile";

import GatewayCard from "../components/gateway/Card";

const GatewayOrganizations = () => {
    const { windowInnerHeight } = useModel("global");
    const { loading, logFiles, loadAllLogFiles } = useAllLogFiles();
    let { loading: contentLoading, loadLogContent, logContent, setLogContent } = useLogContent();
    let { loading: deleteLoading, deleteLogFile } = useDeleteLogFile();

    const state = useReactive({
        selectedLogFile: "",
        isDeleteLogFile: false,
    });

    useEffect(() => {
        if (state.selectedLogFile) {
            let logDate = state.selectedLogFile.filename.split(".").at(-1);
            state.isDeleteLogFile = dayjs(logDate).isValid();
            loadLogContent(state.selectedLogFile.filename);
        }
    }, [state.selectedLogFile]);

    if (!state.selectedLogFile) logContent = "";

    const cardBodyHeight = windowInnerHeight - 90 - 20 - 54;

    return (
        <div style={{ padding: 10 }}>
            <Row gutter={10}>
                <Col span={5}>
                    <GatewayCard cardBodyHeight={cardBodyHeight} />
                </Col>
                <Col span={6}>
                    <SemiCard
                        title={<span style={{ lineHeight: "32px", fontSize: 15 }}>日志文件列表</span>}
                        headerExtraContent={<Space split={<Divider type="vertical" />} size={10}></Space>}
                        headerStyle={{ padding: "10px 14px" }}
                        shadows="always"
                        bordered={false}
                        bodyStyle={{ padding: 1, height: cardBodyHeight - 2, overflow: "auto" }}
                    >
                        <List
                            loading={loading}
                            dataSource={logFiles}
                            renderItem={(item, index) => (
                                <List.Item
                                    key={index}
                                    className={`list-item ${state.selectedLogFile?.filename === item?.filename ? "list-item-selected" : ""}`}
                                    main={<div style={{ color: "var(--semi-color-text-0)", fontWeight: 500 }}>{item?.filename || "-"}</div>}
                                    extra={helper.byteConvert(item.size)}
                                    onClick={() => (state.selectedLogFile = item)}
                                ></List.Item>
                            )}
                        />
                    </SemiCard>
                </Col>
                <Col span={13}>
                    <SemiCard
                        title={<span style={{ lineHeight: "32px", fontSize: 15 }}>日志文件内容</span>}
                        headerExtraContent={
                            <ButtonGroup>
                                {state.isDeleteLogFile && logContent && (
                                    <Popconfirm
                                        placement="topRight"
                                        title="确认要删除该日志文件吗?"
                                        okButtonProps={{ loading: deleteLoading }}
                                        onConfirm={() =>
                                            deleteLogFile(state.selectedLogFile.filename, (res) => {
                                                if (res) {
                                                    message.success("成功删除日志文件");
                                                    setLogContent("");
                                                    loadAllLogFiles();
                                                }
                                            })
                                        }
                                    >
                                        <Button icon={<IconDelete style={{ color: "#E91E63" }} />}></Button>
                                    </Popconfirm>
                                )}
                            </ButtonGroup>
                        }
                        headerStyle={{ padding: "10px 14px" }}
                        shadows="always"
                        bordered={false}
                        bodyStyle={{ padding: 0, height: cardBodyHeight - 2 }}
                    >
                        <pre className="log-file-pre" style={{ padding: "10px 30px", height: cardBodyHeight }}>
                            <Spin spinning={contentLoading}>{logContent ? logContent : <Empty />}</Spin>
                        </pre>
                    </SemiCard>
                </Col>
            </Row>
        </div>
    );
};

export default GatewayOrganizations;
